
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */
package com.hlkj.pay.app.common.ext.tencent;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.extension.annotation.Extension;
import com.hlkj.pay.app.common.OcrExtPt;
import com.hlkj.pay.common.CommonResultCode;
import com.hlkj.pay.app.common.ext.CommonExtConstant;
import com.hlkj.pay.dto.common.ocr.OcrScan;
import com.hlkj.pay.dto.common.ocr.OcrScanIdFront;
import com.hlkj.pay.dto.common.ocr.OcrScanResult;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
/**
 * @author monk@techsun.com
 * @date 2023/04/13 11:58
 */
@Slf4j
@Repository
@RequiredArgsConstructor
@Extension(bizId = CommonExtConstant.CHANNEL_TENCENT,useCase = CommonExtConstant.OCR_ID_FRONT)
public class TencentOcrIDFrontExtPt extends TencentCommon implements OcrExtPt {
    @Override
    public CommonResult<OcrScanResult> ocrScan(OcrScan ocrScan) {
		try{
			OcrClient client = ocrClient(ocrScan.getOcrConfig());
			// 实例化一个请求对象,每个接口都会对应一个request对象
			IDCardOCRRequest req = new IDCardOCRRequest();
			req.setImageUrl(ocrScan.getImageUrl());
			// 返回的resp是一个IDCardOCRResponse的实例，与请求对象对应
			IDCardOCRResponse resp = client.IDCardOCR(req);
			if(resp==null){
				log.error("腾讯云 身份证正面 orc识别失败：返回信息位空");
				return CommonResult.error(CommonResultCode.OCR_ERROR);
			}
			OcrScanIdFront ocrScanIdFront =new OcrScanIdFront();
			ocrScanIdFront.setAddress(resp.getAddress());
			ocrScanIdFront.setBirthDate(resp.getBirth());
			ocrScanIdFront.setEthnicity(resp.getNation());
			ocrScanIdFront.setIdNumber(resp.getIdNum());
			ocrScanIdFront.setName(resp.getName());
			ocrScanIdFront.setSex(resp.getSex());
			
			return CommonResult.success(ocrScanIdFront);
		} catch (TencentCloudSDKException error) {
			log.error("腾讯云 身份证正面 orc识别失败：message",error.getMessage(),error);
			return CommonResult.error(CommonResultCode.OCR_ERROR.getCode(),error.getMessage());
		}
	}
}
